/*
 * 文件名：ArticleController.java	 
 * 时     间：上午11:32:38
 * 作     者：liyf      
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.qianhai.news.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.controller.BaseController;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.utils.Tools;
import com.qianhai.news.model.Article;
import com.qianhai.news.model.ArticleExample;
import com.qianhai.news.model.Articletype;
import com.qianhai.news.model.ArticletypeExample;
import com.qianhai.news.page.ArticlePage;
import com.qianhai.news.page.ArticletypePage;
import com.qianhai.news.service.face.ArticleService;
import com.qianhai.news.service.face.ArticletypeService;
import com.qianhai.news.vo.ArticleTypeVo;

/**
 * @ClassName: ArticleController
 * @Description: 统一文章管理,包括文件类型、文章
 * @author liyf
 * @date 2014年11月14日 上午11:32:38
 */
@RestController
@RequestMapping(value = "/article", method = RequestMethod.POST)
public class ArticleManagerController extends BaseController
{
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticletypeService articleTypeService;
	
	/**
	 * 
	 * @方法名称: saveArticleType
	 * @描述: 添加文章分类 必填字段： UserUuid ArticletypeName ArticletypeState
	 *      ArticletypeType ArticletypeTypecn
	 * 
	 *
	 * @参数:
	 * @param type
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/saveArticleType.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap saveArticleType(@RequestBody Articletype type)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			articleTypeService.save(type);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: updateArticleType
	 * @描述: 根据ID修改文章类别 必传字段：articletypeUuid
	 * 
	 *
	 * @参数:
	 * @param type
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/updateArticleType.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap updateArticleType(@RequestBody Articletype type)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			articleTypeService.update(type);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryListType
	 * @描述: 分页查询文章列表 必传字段：{"currentPage":1,"pageSize":1}
	 *
	 * @参数:
	 * @param page
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryListType.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryListType(@RequestBody ArticletypePage page)
	{
		ModelMap resultMap = new ModelMap();
		 
		try
		{
			articleTypeService.queryByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryTypeByUuid
	 * @描述: 根据UUID查询出文章分类
	 *
	 * @参数:
	 * @param uuid
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryTypeByUuid.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryTypeByUuid(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if (null == uuid)
			{
				throw new BaseException("30000-00000-0016");
			}
			
			Articletype type = articleTypeService.selectByPrimaryKey(uuid);
			resultMap.put(CommonContans.STRUTS_DATA, type);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryTypeByCode 
	 * @描述: 
	 *     根据code 跟 语言  来查询到单个分类
	 *
	 * @参数:
	 *    @param code
	 *    @param language
	 *    @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryTypeByCode.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryTypeByCode(String code,String language)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if (Tools.isEmpty(code))
			{
				throw new BaseException("30000-00000-0026");
			}
			if (Tools.isEmpty(language))
			{
				throw new BaseException("30000-00000-0027");
			}
			ArticletypeExample ate = new ArticletypeExample();
			ate.or().andArticletypeCodeEqualTo(code).andArticletypeLanguageEqualTo(language);
			List<Articletype> list = articleTypeService.selectByCondition(ate);
			Articletype type = new Articletype();
			if(list !=null && list.size()>0){
				type = list.get(0);
			}
			
			resultMap.put(CommonContans.STRUTS_DATA, type);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/deleteTypeByUuid.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteTypeByUuid(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if (null == uuid)
			{
				throw new BaseException("30000-00000-0016");
			}
			
			articleTypeService.deleteByPrimaryKey(uuid);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: deleteTypeByCode 
	 * @描述: 
	 *     根据code来删除分类 
	 *     会把所有语言全部删除
	 *
	 * @参数:
	 *    @param code
	 *    @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/deleteTypeByCode.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteTypeByCode(String code)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if (Tools.isEmpty(code))
			{
				throw new BaseException("30000-00000-0026");
			}
			ArticletypeExample ate = new ArticletypeExample();
			ate.or().andArticletypeCodeEqualTo(code);
			articleTypeService.deleteByCondition(ate);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: saveArticle
	 * @描述: 添加文章
	 *
	 * @参数:
	 * @param article
	 *            {"articleUuid":null,"userUuid":null,"articletypeUuid":null,
	 *            "articleTitle"
	 *            :null,"articleIntro":null,"articlePhone":null,"articleWeb"
	 *            :null,"articleState":null,"articleIntroimg":null,
	 *            "articleCreatetime":null,"articleContent":null}
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/saveArticle.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap saveArticle(@RequestBody Article article)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			articleService.addArticle(article);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: modifyArticle
	 * @描述: 修改
	 *
	 * @参数:
	 * @param article
	 *            {"articleUuid":null,"userUuid":null,"articletypeUuid":null,
	 *            "articleTitle"
	 *            :null,"articleIntro":null,"articlePhone":null,"articleWeb"
	 *            :null,"articleState":null,"articleIntroimg":null,
	 *            "articleCreatetime":null,"articleContent":null}
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/modifyArticle.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap modifyArticle(@RequestBody Article article)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			articleService.modifyArticle(article);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryArticle
	 * @描述: 在这里简单描述方法作用
	 *
	 * @参数:
	 * @param page
	 *            {"currentPage":1,"pageSize":1,"title":"文章标题","articleTypeId":
	 *            "文章类型ID"}
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryArticle.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryArticle(@RequestBody ArticlePage page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			articleService.queryByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: deleteArticle
	 * @描述: 删除文章
	 *
	 * @参数:
	 * @param uuid
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/deleteArticle.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteArticle(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			articleService.deleteByPrimaryKey(uuid);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}

	/**
	 * 
	 * @方法名称: deleteArticleByCode 
	 * @描述: 
	 *     根据code来删除文章
	 *     会把code下所有语言的全部删除
	 *
	 * @参数:
	 *    @param code
	 *    @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/deleteArticleByCode.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap deleteArticleByCode(String code)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			ArticleExample ae = new ArticleExample();
			ae.or().andArticleCodeEqualTo(code);
			
			articleService.deleteByCondition(ae);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryArticleByKey
	 * @描述: 通过关键字查询
	 *
	 * @参数:
	 * @param uuid
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryArticleByKey.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryArticleByKey(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			Article article = articleService.selectByPrimaryKey(uuid);
			
			resultMap.put(CommonContans.STRUTS_DATA, article);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryArticleByCode 
	 * @描述: 
	 *     根据文章code跟语言类型来查找到唯一的文章
	 *
	 * @参数:
	 *    @param code
	 *    @param language
	 *    @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryArticleByCode.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryArticleByCode(String code,String language)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			ArticleExample ae = new ArticleExample();
			ae.or().andArticleCodeEqualTo(code).andArticleLanguageEqualTo(language);
			List<Article> list = articleService.selectByCondition(ae);
			
			Article article = new Article();
			if(list!=null && list.size()>0){
				article = list.get(0);
			}
			resultMap.put(CommonContans.STRUTS_DATA, article);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryTypeTree 
	 * @描述: 
	 *     把分类的各种语言封装到同一条数据 并封装成树型
	 *
	 * @参数:
	 *    @param page
	 *    @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryTypeTree.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryTypeTree(@RequestBody ArticletypePage page)
	{
		ModelMap resultMap = new ModelMap();
		List<ArticleTypeVo> list = new ArrayList<ArticleTypeVo>();
		try
		{
			list = articleTypeService.queryTypeListArticle(page);
			resultMap.put(CommonContans.STRUTS_DATA, list);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryTypeTree 
	 * @描述: 
	 *     把分类的一种语言封装成树型
	 *
	 * @参数:
	 *    @param page
	 *    @return
	 *
	 * @返回类型: ModelMap
	 * @作者:chenyi
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryTypeLanguageTree.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryTypeLanguageTree(@RequestBody ArticletypePage page)
	{
		ModelMap resultMap = new ModelMap();
		List<Articletype> list = new ArrayList<Articletype>();
		try
		{
			list = articleTypeService.queryTypeLanguageTree(page);
			resultMap.put(CommonContans.STRUTS_DATA, list);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
}
